Welcome.
In this lecture, we're going to disable player collisions between players by having all the descendant
based parts in a player's character be set to the player's collision group.
So if you remember when we first set up our game, if we go to the model tab and we open up collision
groups, we created a few collision groups and one of them was a player's collision group.
And this player's collision group cannot collide with other players.
So that means we need to go through all of the limbs inside of our player's character, set them to
this collision group to prevent other players from colliding with each other.
So to get started, we can go ahead and create a new script inside of our server script service and
we can call this our player collision handler.
We'll use the same template as before, and we're going to go ahead and grab the player service.
And from here we can go ahead and listen for when a player joins our game.
It's because we want to listen to when a player has a character added to them, and we can go ahead
and connect a function or create a dedicated function for when a character is added.
We can call this on character added and get that character, and we'll connect on character added to
this player's character added event.
You could create a lambda function here instead, but that means you'll be creating this lambda function
for every single player that joins your game.
So if you want to be more memory efficient, instead we can create a single function outside and then
refer or just connect the single function to every single player that has a character added.
Then what we would want to do when a player has a character added is we want to loop through all of
the descendants inside of that character and find all the bass parts, set them to the correct collision
group, and then we also want to listen for when a descendant is added or removed from our character
model, and if that descendant that's being added or removed is a bass part, then we want to go ahead
and update the collision group for that part as well.
So let's go ahead and create a couple more functions.
One function we'll have is we'll listen for when a descendant is added to our character.
And we'll get that descendant.
And then we also want have a function to listen for when a descendant is removed.
And we'll get that descendant as well.
So from this point, what we can go ahead and do is listen to our character dot descendant added event
and we'll connect our on descendant added function.
And then for our character dot descendant removing event will also connect our on a descendant removed
function.
And these are going to be very simple functions.
We're just going to check if this descendant is a base part.
So if this descendant is not a base part so is a base part then we're just going to return.
Otherwise we can go ahead and update this descendants collision group property to be equal to.
And since this descendant is being removed, we want to update it to the default collision group, which
is just the string of default.
Otherwise, we can copy this.
And when a descendant is added to a player's character, we want to set the collision group to the player's
collision group.
Now we can just loop through every single part inside of our character, and we're going to use the
Get descendants function.
And we can basically do exactly what we did for the descendant added event.
But instead of copying this and pasting that there, we could literally just call our on descendant
added function and just pass the part from this for loop.
So now every single player in our game, when they have a character added, we're going to force all
of the base parts to have the player's collision group.
And then when a descendant is removed from our player's character, then we're going to place it back
in the default collision group.
And I've placed these functions in the wrong section for my comments.
There is no public functions here, so I'm just going to delete that and put it there.
And now we should have our collision groups completely set up for our players.
So to completely test this out, we can go ahead and set up a two player server and see if our players
collide with each other.
So here I have both of my players in the game, and if I go and walk towards this player, as you can
see, I pass right through them because both of these players have the players collision group and we
can no longer collide with each other.
This prevents players from being able to stack on top of each other and trying to escape the map, and
it also prevents just players from colliding with other players in general.
I hope you're enjoying the project so far and I'll see you in the next lecture.